package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据动态id和类型，查询互动数据
    public List<Comment> findComments(String movementId, Integer commentType,
                                      Integer page, Integer pagesize) {
        //构建Criteria
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType);
        //构建Query
        Query query = Query.query(criteria)
                        .with(Sort.by(Sort.Order.desc("created")))
                        .skip((page-1)*pagesize)
                        .limit(pagesize);
        return mongoTemplate.find(query,Comment.class);
    }

    //保存互动数据，返回互动总数
    public Integer save(Comment comment) {
        Integer commentType = comment.getCommentType(); //互动类型
        //a、保存互动数据
        ObjectId movementId = comment.getPublishId();
        //查询动态对象
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //设置动态发布人id到comment
        comment.setPublishUserId(movement.getUserId());
        //保存
        mongoTemplate.save(comment);

        //b、更新动态，获取最新的数据
        //条件
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        //更新字段
        Update update = new Update();
        if(commentType == 1) {
            update.inc("likeCount",1);
        }else if (commentType ==2) {
            update.inc("commentCount",1);
        }else{
            update.inc("loveCount",1);
        }
        //更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true); //当更新成功之后，获取最新的数据对象
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);//查询条件，更新的字段内容,配置，实体类
        if(commentType == 1) {
            return modify.getLikeCount();
        }else if (commentType ==2) {
            return modify.getCommentCount();
        }else{
            return modify.getLoveCount();
        }
    }

    //删除互动数据，返回互动总数
    public Integer delete(Long userId, Integer commentType, String movementId) {
        //i、删除互动数据 delete from tab where userId=? and commentType=? and movementId=?
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("commentType").is(commentType)
                .and("publishId").is(new ObjectId(movementId));
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);
        //ii、更新动态表中的总数字段，获取最新值
        //1、条件
        Criteria movementCriteria = Criteria.where("id").is(new ObjectId(movementId));
        Query movementQuery = Query.query(movementCriteria);
        //2、更新的字段
        Update update = new Update();
        if(commentType == 1) {
            update.inc("likeCount",-1);
        }else if (commentType ==2) {
            update.inc("commentCount",-1);
        }else{
            update.inc("loveCount",-1);
        }
        //3、更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //4、更新获取最新值
        Movement modify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);//更新之后获取最新的数据（query，update，options，实体类）
        if(commentType == 1) {
            return modify.getLikeCount();
        }else if (commentType ==2) {
            return modify.getCommentCount();
        }else{
            return modify.getLoveCount();
        }
    }

//    //保存互动数据，返回互动总数
//    public Integer save(Comment comment) {
//        //a、保存互动数据
//        ObjectId movementId = comment.getPublishId();
//        //查询动态对象
//        Movement movement = mongoTemplate.findById(movementId, Movement.class);
//        //设置动态发布人id到comment
//        comment.setPublishUserId(movement.getUserId());
//        //保存
//        mongoTemplate.save(comment);
//        //b、更新动态中的评论总数：update xx set commentCount = commentCount+1 where id =xxx
//        //条件
//        Criteria criteria = Criteria.where("id").is(movementId);
//        Query query = Query.query(criteria);
//        //更新字段
//        Update update = new Update();
//        update.inc("commentCount",1);
//        mongoTemplate.updateFirst(query,update,Movement.class) ;//查询条件，更新的字段内容，实体类
//        //c、返回最新互动总数
//        Movement movement1 = mongoTemplate.findById(movementId, Movement.class);
//        return movement1.getCommentCount();
//    }
}
